/*
 * Project it.battlehorse.rcp.tools
 * Created on Nov 18, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */
package it.battlehorse.rcp.tools.log.actions;

import it.battlehorse.rcp.tools.log.LogActivator;
import it.battlehorse.rcp.tools.log.views.ActorLogContentProvider;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.ui.PlatformUI;

/**
 * The action which is responsible for deleting the currently selected log element.
 * The action responds also when the {@code SWT.DEL} key is pressed.
 * 
 * @author battlehorse
 * @since Nov 27, 2005
 */
public class DeleteCurrentLogAction extends Action implements KeyListener {
	
	private ActorLogContentProvider provider;
	
	private TableViewer viewer;
	
	public DeleteCurrentLogAction(ActorLogContentProvider provider, TableViewer viewer) {
		this.provider = provider;
		this.viewer = viewer;
		
		super.setText("&Delete current log item");
		super.setImageDescriptor(
				LogActivator.getImageDescriptor("resources/delete.png"));
		super.setToolTipText("Delete current log item");
	}
	
	@Override
	public void run() {
		IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
		if (sel != null && sel.getFirstElement() != null) {
			
			boolean delete = MessageDialog.openQuestion(
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
					"Delete current log item",
					"Do you really want to delete the current log item?");
			
			if (delete) {
				
				provider.deleteItem(sel.getFirstElement());
			}
			
		}
		
		
	}
	
	public void keyPressed(KeyEvent e) {
		// nothing to do
	}
	
	public void keyReleased(KeyEvent e) {
		if (e.keyCode == SWT.DEL) {
			run();
		}
	}
	
	
	

}
